<?php
// 抓取首页
namespace App\Models\Traits;

use App\Models\Tieba;
use Carbon\Carbon;
use Cache;
use DB;
use App\Jobs\TiebaGetIndex;
use Illuminate\Support\Facades\Log;
trait GetIndexHelper 
{
	protected $cache_key = 'tieba_name_lists';
	protected $cache_expire = 30;
	
	public function DownIndexHtml() 
	{
		
		$arr = $this->getTiebaByCache();
		if ($arr) {
			foreach($arr as $name){
				// 在这里推送任务
				dispatch(new TiebaGetIndex($name));
			}
		}
	}

	private function getTiebaByCache() 
	{
		// 从缓存中获取 贴吧名称数组
		return Cache::remember($this->cache_key, $this->cache_expire, function(){
            return $this->TiebaNameLists();
        });
	}

	private function TiebaNameLists()
	{
		return Tieba::where('isget', 1)->pluck('name');
	}
}